package com.hgnu.dorm.util;

import cn.dev33.satoken.stp.StpInterface;
import com.hgnu.dorm.domain.po.RolePermission;
import com.hgnu.dorm.domain.po.Student;
import com.hgnu.dorm.domain.po.User;
import com.hgnu.dorm.mapper.PermissionMapper;
import com.hgnu.dorm.mapper.RolePermissionMapper;
import com.hgnu.dorm.mapper.StudentMapper;
import com.hgnu.dorm.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 自定义权限加载接口实现类
 */
@Component    // 保证此类被 SpringBoot 扫描，完成 Sa-Token 的自定义权限验证扩展
public class StpInterfaceImpl implements StpInterface {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private RolePermissionMapper rolePermissionMapper;
    @Autowired
    private PermissionMapper permissionMapper;

    /**
     * 返回一个账号所拥有的权限码集合
     */
    @Override
    public List<String> getPermissionList(Object loginId, String loginType) {
        List<String> list = new ArrayList<String>();
        String[] split = loginId.toString().split(",");
        String id = split[0];
        String currentIdentity = split[1];
        if(currentIdentity.equals("admin")){
            User u = userMapper.getById(Integer.parseInt(id));
            Integer roleId = u.getRoleId();
            List<RolePermission> rolePermissions = rolePermissionMapper.findAllByRoleId(roleId.longValue());
            List<Long> permissionIds = rolePermissions.stream()
                    .map(RolePermission::getPermissionId)
                    .collect(Collectors.toList());
            permissionMapper.findPermissionsByIds(permissionIds).forEach(p -> list.add(p.getPerms()));
            return list;
        }else {
            Student s = studentMapper.selectStudentById(Integer.parseInt(id));
            Integer roleId = s.getRoleId();
            List<RolePermission> rolePermissions = rolePermissionMapper.findAllByRoleId(roleId.longValue());
            List<Long> permissionIds = rolePermissions.stream()
                    .map(RolePermission::getPermissionId)
                    .collect(Collectors.toList());
            permissionMapper.findPermissionsByIds(permissionIds).forEach(p -> list.add(p.getPerms()));
            return list;
        }

    }

    /**
     * 返回一个账号所拥有的角色标识集合 (权限与角色可分开校验)
     */
    @Override
    public List<String> getRoleList(Object loginId, String loginType) {
        List<String> list = new ArrayList<String>();
        String[] split = loginId.toString().split(",");
        String id = split[0];
        String currentIdentity = split[1];
        if(currentIdentity.equals("admin")){
            User u = userMapper.getById(Integer.parseInt(id));
            Integer roleId = u.getRoleId();
            if(roleId == 0){
                list.add("admin");
            }else{
                list.add("common");
            }
            return list;
        }else{
            list.add("student");
            return list;
        }
    }

}

